const { UUIDV4, UUID, STRING, TINYINT, DATE, Model } = require('sequelize');
const UserSafe = require('./user-safe');
const Address = require('./address');
const Cart = require('../order/cart');
const GoodsComment = require('../goods/goods-comment');
const db = require('../../helpers/db');

class User extends Model {}

User.init(
    {
        id: {
            type: UUID,
            defaultValue: UUIDV4,
            primaryKey: true,
            comment: '用户ID'
        },
        nick: {
            type: STRING(32),
            comment: '用户昵称'
        },
        gender: {
            type: TINYINT(1),
            defaultValue: 0,
            comment: '用户性别：0保密，1男，2女'
        },
        avatar: {
            type: STRING(128),
            comment: '用户头像'
        },
        birthday: {
            type: DATE,
            defaultValue: new Date(),
            comment: '用户出生日期'
        }
    },
    {
        tableName: 'user',
        underscored: true,
        sequelize: db
    }
);

User.hasOne(UserSafe, {
    foreignKey: 'user'
});

User.hasMany(Address, {
    foreignKey: 'user'
});

User.hasMany(Cart, {
    foreignKey: 'user'
});

User.hasMany(GoodsComment, {
    foreignKey: 'fromUser'
});

module.exports = User;
